using System.Collections;
using UnityEngine;

public class CamMove : MonoBehaviour
{
	public Transform body;

	public bool isdied;

	public float angleX;

	public float angleY;

	private Transform m_transform;

	public float rotate_xdiss;

	private float angle_curx;

	private float target_curx;

	private float target_cury;

	private float angle_cury;

	public Transform camtra;

	private Vector3 cam_startpos;

	private bool diedflag;

	private Vector3 forwardtemp;

	private Vector3 dircam;

	private Vector3 startpos;

	private float movetime;

	private float rotatespeed;

	private Vector3 lookpos;

	private void Start()
	{
		m_transform = base.transform;
		cam_startpos = camtra.localPosition;
	}

	private void Update()
	{
		if (diedflag)
		{
			movetime += Time.deltaTime;
			lookpos = Vector3.Lerp(lookpos, body.position, Time.deltaTime * 8f);
			camtra.LookAt(lookpos);
			if (movetime <= 1.4f)
			{
				camtra.position = startpos + movetime / 1.5f * dircam;
			}
			else if (movetime <= 4f)
			{
				camtra.Translate(rotatespeed * Time.deltaTime, 0f, 0f, Space.Self);
			}
		}
	}

	private void LateUpdate()
	{
		if (!groundtrigger.triggered && !isdied && !GlobalInf.gameover)
		{
			if (GlobalInf.PlaneCtrlType == PLANECTRLTYPE.BUTTON)
			{
				target_curx = (0f - Joystick.joystickAxis.y) * angleX;
				target_cury = (0f - Joystick.joystickAxis.x) * angleY;
			}
			else
			{
				target_curx = (0f - Joystick.gravityAxis.x) * angleX;
				target_cury = (0f - Joystick.gravityAxis.y) * angleY;
			}
			Vector3 localEulerAngles = m_transform.localEulerAngles;
			angle_curx = localEulerAngles.x;
			Vector3 localEulerAngles2 = m_transform.localEulerAngles;
			angle_cury = localEulerAngles2.y;
			angle_curx = Mathf.LerpAngle(angle_curx, target_curx, Time.deltaTime * rotate_xdiss);
			angle_cury = Mathf.LerpAngle(angle_cury, target_cury, Time.deltaTime * rotate_xdiss);
			m_transform.localRotation = Quaternion.Euler(angle_curx, angle_cury, 0f);
		}
	}

	public void diedmove()
	{
		if (!isdied)
		{
			isdied = true;
			forwardtemp = body.right;
			if (forwardtemp.y > 0f)
			{
				forwardtemp.y = 0f;
				forwardtemp = -forwardtemp.normalized;
				rotatespeed = -20f;
			}
			else
			{
				forwardtemp.y = 0f;
				forwardtemp = forwardtemp.normalized;
				rotatespeed = 20f;
			}
			UIController.instance.disPlayeraim();
			plane_smothfollow.instance.set_speedpos();
			startpos = camtra.position;
			movetime = 0f;
			dircam = body.position + Vector3.up * 30f + forwardtemp * 50f - camtra.position;
			lookpos = camtra.parent.position;
			diedflag = true;
		}
	}

	private IEnumerator dieanima()
	{
		for (float t2 = 0f; t2 < 1.5f; t2 += Time.deltaTime)
		{
			if (isdied)
			{
				movetime += Time.deltaTime;
				lookpos = Vector3.Lerp(lookpos, body.position, Time.deltaTime * 5f);
				camtra.LookAt(lookpos);
				if (movetime > 1.5f)
				{
					movetime = 1.5f;
				}
				camtra.position = startpos + movetime / 1.5f * dircam;
			}
			yield return 0;
		}
		for (float t = 0f; t < 2f; t += Time.deltaTime)
		{
			if (isdied)
			{
				camtra.LookAt(lookpos);
				camtra.Translate(rotatespeed * Time.deltaTime, 0f, 0f, Space.Self);
			}
			yield return 0;
		}
	}

	public void reset()
	{
		isdied = false;
		diedflag = false;
		camtra.localPosition = cam_startpos;
		camtra.LookAt(m_transform);
		UIController.instance.appearPlayeraim();
	}
}
